#include <func.h>

void cleanup1(void *p)
{
    printf("I am child thread,start free\n");
    free(p);
    printf("free success\n");
}

void* thread_func(void *p)
{
    void *p1=malloc(20);
    pthread_cleanup_push(cleanup1,p1);
    sleep(1);
    printf("I am child,正常退出\n");
    pthread_exit(NULL);
    pthread_cleanup_pop(1);
}

//子线程通过pthread_exit退出，会去执行cleanup清理函数，来free对应的空间
int main()
{
    pthread_t th_id;
    int ret=pthread_create(&th_id,NULL,thread_func,NULL);
    THREAD_ERR_CHECK(ret,"pthread_creat");
    printf("I am main thread\n");

    long thread_ret;
    ret=pthread_join(th_id,(void**)&thread_ret);
    THREAD_ERR_CHECK(ret,"pthread_join");
    printf("I am main thread %ld",thread_ret);
    return 0;
}